/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;

import com.stip.server.entities.Activeties;
import com.stip.server.entities.Locations;
import com.stip.server.entities.Organization;
import com.stip.server.entities.UserAct;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class ActDA {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext 
    EntityManager em;
    public Activeties findActivetiesByID(Integer id){
    Activeties activeties=em.find(Activeties.class, id);
    return  activeties;
    }
    public Integer createActivety(Integer firstOrg,Integer secondOrg,Integer thirdOrg,Integer firstEnsure,Integer secondEnsure,Integer thirdEnsure,Date validity,Integer location,String abstracts,Date actDate,String actUrl,String email,Integer status){
    Activeties activeties=new Activeties();
    Organization fOrganization=em.find(Organization.class, firstOrg);
    activeties.setFirstOrg(fOrganization);
    Organization sOrganization=em.find(Organization.class, secondOrg);
    activeties.setSecondOrg(sOrganization);
    Organization tOrganization=em.find(Organization.class, thirdOrg);
    activeties.setThirdOrg(tOrganization);
    activeties.setFirstEnsure(firstEnsure);
    activeties.setSecondEnsure(secondEnsure);
    activeties.setThirdEnsure(thirdEnsure);
    activeties.setValidity(validity);
    activeties.setUrl(actUrl);
    activeties.setEmail(email);
    activeties.setStatus(status);
    Locations locations=em.find(Locations.class, location);
    activeties.setLocation(locations);
    activeties.setAbstracts(abstracts);
    activeties.setActDate(actDate);
    List<UserAct> userActs=new ArrayList<UserAct>();
    em.persist(activeties);
    em.flush();
    Query query=em.createQuery("SELECT u FROM UserAct u WHERE u.actId = :actId");
    query.setParameter("actId", activeties);
    List list=query.getResultList();
        for (Iterator it = list.iterator(); it.hasNext();) {
             UserAct object = (UserAct)it.next();
             userActs.add(object);            
        }
       activeties.setUserActList(userActs);
        em.merge(activeties);
        
    return  activeties.getId();
    }
    public Boolean editActivety(Integer id,Integer firstOrg,Integer secondOrg,Integer thirdOrg,Integer firstEnsure,Integer secondEnsure,Integer thirdEnsure,Date validity,Integer location,String abstracts,Date actDate,String actUrl,String email,Integer status){
    Activeties activeties=em.find(Activeties.class, id);
    
        if (activeties!=null) {
            if (firstOrg==null) {
                activeties.setFirstOrg(activeties.getFirstOrg());
            } else {
                
                activeties.setFirstOrg(em.find(Organization.class, firstOrg));
            }
            if (status!=null) {
                activeties.setStatus(activeties.getStatus());
            } else {
                activeties.setStatus(status);
            }
            if (actUrl!=null) {
                activeties.setUrl(activeties.getUrl());
            } else {
                activeties.setUrl(actUrl);
            }
            if (email!=null) {
                activeties.setEmail(activeties.getEmail());
            } else {
                activeties.setEmail(actUrl);
            }
            if (secondOrg==null) {
                activeties.setSecondOrg(activeties.getSecondOrg());
            } else {
                activeties.setSecondOrg(em.find(Organization.class, secondOrg));
            }
            if (thirdOrg==null) {
                activeties.setThirdOrg(activeties.getThirdOrg());
            } else {
                activeties.setThirdOrg(em.find(Organization.class, thirdOrg));
            }
            if (firstEnsure==null) {
                activeties.setFirstEnsure(activeties.getFirstEnsure());
            } else {
                activeties.setFirstEnsure(firstEnsure);
            }
            if (secondEnsure==null) {
                activeties.setSecondEnsure(activeties.getSecondEnsure());
            } else {
                activeties.setSecondEnsure(secondEnsure);
            }
            if (thirdEnsure==null) {
                activeties.setThirdEnsure(activeties.getThirdEnsure());
            } else {
                activeties.setThirdEnsure(thirdEnsure);
            }
            if (validity==null) {
                activeties.setValidity(activeties.getValidity());
            } else {
                activeties.setValidity(validity);
            }
            if (location==null) {
                activeties.setLocation(activeties.getLocation());
            } else {
                activeties.setLocation(em.find(Locations.class, location));
            }
            if (abstracts==null) {
                activeties.setAbstracts(activeties.getAbstracts());
            } else {
                activeties.setAbstracts(abstracts);
            }
            if (actDate==null) {
                activeties.setActDate(activeties.getActDate());
            } else {
                activeties.setActDate(actDate);
            }
            em.merge(activeties);
            return  true;
        } else {
            return false;
        }
    }
    public Boolean deleteActivety(Integer id){
    Activeties activeties=em.find(Activeties.class, id);
        if (activeties!=null) {
            em.remove(activeties);
            return  true;
        } else {
            return  false;
        }
    }
}
